//
//  HJSingleChatSettingController.m
//  KaiChat
//
//  Created by lx on 2023/11/27.
//

#import "HJSingleChatSettingController.h"
#import "HJTitleCell.h"
#import "HJSwitchCell.h"
#import "HJReportController.h"
#import "HJMinePresent.h"
#import "HJIM.h"
#import "HJUIAlertController.h"

@interface HJSingleChatSettingController ()

@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) HJCellModel *blockModel;

@end

@implementation HJSingleChatSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"Profile settings";
    [self.tableView registerClass:HJTitleCell.class forCellReuseIdentifier:@"HJTitleCell"];
    [self.tableView registerClass:HJSwitchCell.class forCellReuseIdentifier:@"HJSwitchCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(KNavBarHight);
        make.bottom.offset(0);
    }];
    
    [self loadData];
}


- (void)loadData {
    [HJIM.shareIM.userManager queryUserInfoWithUserId:self.userId complete:^(HJChatUserModel * _Nullable model) {
        if (model.isFridend) {
            HJChatFriendModel *chatFriendModel = [HJIM.shareIM.userManager queryFriendInfoByFriendId:self.userId];
            self.blockModel.isOn = chatFriendModel.status == 2;
        } else {
            self.blockModel.isOn = model.relationship == 4;
        }
        [self.tableView reloadData];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    HJCellModel *model = self.datas[indexPath.row];
    
    if ([model.identifier isEqualToString:@"block"]) {
        HJSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJSwitchCell" forIndexPath:indexPath];
        cell.model = model;
        TJWeakify(self)
        [cell setSwitchValueChange:^(UISwitch * _Nonnull switchView) {
            
            if (!switchView.isOn) {
                HJUIAlertController *alert = [HJUIAlertController alertControllerWithTitle:nil message:@"Are you sure you want to add this user to block list? Message will not be received after the process!" preferredStyle:HJUIAlertControllerStyleActionSheet];
                [alert addAction:[HJUIAlertAction actionWithTitle:@"OK" style:HJUIAlertActionStyleDestructive handler:^(HJUIAlertAction * _Nonnull action) {
                    [weakself setBacklistWithSwitchView:switchView model:model];
                }]];
                [alert addAction:[HJUIAlertAction actionWithTitle:@"Cancel" style:HJUIAlertActionStyleCancel handler:^(HJUIAlertAction * _Nonnull action) {
                    
                }]];
                
                [alert showInViewController:self];
            } else {
                [weakself setBacklistWithSwitchView:switchView model:model];
            }
            
        }];
        return cell;
    }
       
    HJTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJTitleCell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)setBacklistWithSwitchView:(UISwitch *)switchView model:(HJCellModel *)model {
    
    [HJHUD showGraceTimeLoadingInView:self.view];
    [HJMinePresent setBacklistWithUserIds:@[self.userId] backlist:!switchView.isOn complete:^(NSError * _Nullable error) {
        if (error) {
            [HJHUD showErrorMessageTip];
            return;
        }
 
        model.isOn = !switchView.isOn;
        [switchView setOn:!switchView.isOn animated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:backlistStateChangeNotification object:self.userId];

        if (self.setBlocklistComplete) {
            self.setBlocklistComplete(model.isOn);
        }
        
        if (model.isOn) {
            [HJHUD showSucceedMessageTip];
        } else {
            [HJHUD showMessage:@"Removed from block list"];
        }
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJCellModel *model = self.datas[indexPath.row];
    if ([model.identifier isEqualToString:@"Report"]) {
        HJReportController *reportController = [[HJReportController alloc] init];
        reportController.userId = self.userId;
        [reportController showInController:self];
        return;
    }
    
}

- (NSArray *)datas {
    if (!_datas) {
        _datas = @[
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.identifier = @"block";
                model.text = @"Block";
                model.showLine = NO;
                self.blockModel = model;
            }],
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.text = @"Report";
                model.identifier = @"Report";
                model.showLine = NO;
            }],
        ];
        
        
    }
    return _datas;
}

@end
